package auth

import (
	"MembershipRegist/controllers/ctrl"
	"MembershipRegist/db"
	"MembershipRegist/models"
	"errors"
)

type AdminController struct {
	ctrl.Controller
	Admin   *models.Admin
	AdminID uint
}

var (
	ErrAdminNotLogin = errors.New("admin not login")
)

func (a *AdminController) Prepare() {
	adminID := a.GetSession("admin_id")
	if adminID == nil {
		a.Abort("403")
		return
	}
	var admin models.Admin
	if value := db.DB.Where("id = ?", adminID.(uint)).First(&admin); value.Error != nil {
		a.Abort("403")
		return
	}
	a.Admin = &admin
	a.AdminID = admin.ID
	return
}

func (a *AdminController) AdminInfo() {
	result := make(map[string]interface{})
	result["status"] = 200
	result["message"] = "success"
	result["admin"] = *a.Admin
	a.Data["json"] = result
	a.ServeJSON()
}
